<template>
  <div>
        <van-button
            class="follow-btn"
            type="info"
            color="#3296fa"
            round
            size="small"
            icon="plus"
            v-if="isFollowed"
            @click="onFollowed"
          >关注</van-button>
        <van-button
            class="follow-btn"
            round
            size="small"
            v-else
            @click="onFollowed"
          >已关注
          </van-button>
  </div>
</template>

<script>
import { addFollowUser, removeFollowUser } from '@/api/user.js'
import { Toast } from 'vant'

export default {
  props: {
    isFollowed: {
      type: Boolean,
      required: true
    },
    autId: {
      type: [Number, Object],
      required: true
    }
  },
  model: {
    prop: 'isFollowed',
    event: 'updaeFollowed'
  },
  data () {
    return {

    }
  },

  created () {

  },

  methods: {
    // 点击关注/取消关注
    async onFollowed () {
      try {
        if (this.isFollowed) {
        // 当前关注 取消关注
          // console.log(1)
          await removeFollowUser(this.autId)
          Toast.success('添加关注成功')
        } else {
        // 当前未关注 关注
          // console.log(2)
          await addFollowUser(this.autId)
          Toast.success('取消关注成功')
        }
        // this.article.is_followed = !this.article.is_followed
        this.$emit('updaeFollowed', !this.isFollowed)
      } catch (err) {
        Toast.fail('操作失败')
      }
    }
  }
}
</script>

<style scoped>

</style>
